<template>
  <a-modal
      :title="!dataForm.id ? '新增' : '修改'"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      okText="保存"
      cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="dataForm" :model="dataForm" :rules="validatorRules">
        <a-form-model-item label="角色名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
          <a-input placeholder="请输入角色名称" v-model="dataForm.name" />
        </a-form-model-item>

        <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
          <a-textarea placeholder="请输入角色描述" v-model="dataForm.remark" auto-size/>
        </a-form-model-item>

        <a-form-model-item label="菜单授权" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="menuIdList">
          <a-tree-select
              v-model="dataForm.menuIdList"
              style="width: 100%"
              :tree-data="menuList"
              :replace-fields="replaceFields"
              tree-checkable
              :show-checked-strategy="SHOW_PARENT"
              search-placeholder="Please select"
          />
        </a-form-model-item>

        <a-form-model-item label="数据授权" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deptIdList">
          <a-tree-select
              v-model="dataForm.deptIdList"
              style="width: 100%"
              :tree-data="deptList"
              :replace-fields="replaceFields"
              tree-checkable
              :show-checked-strategy="SHOW_PARENT"
              search-placeholder="Please select"
          />
        </a-form-model-item>

      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { duplicateCheck, queryRoleById, queryMenuTreeList, queryDepartTreeList } from '@/api/api'
import { saveAction } from '@/api/manage'
import { TreeSelect } from 'ant-design-vue';
const SHOW_PARENT = TreeSelect.SHOW_PARENT;

export default {
  name: "role-form",
  data () {
    return {
      SHOW_PARENT,
      visible: false,
      confirmLoading: false,
      dataForm: {
        id: '',
        name: '',
        remark: '',
        menuIdList: [],
        deptIdList: [],
      },
      menuList: [],
      deptList: [],
      replaceFields: {
        children: 'children',
        title: 'name',
        key: 'id',
        value: 'id'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      url: {
        save: '/sys/role'
      }
    }
  },
  computed: {
    validatorRules() {
      var validateName = (rule, value, callback) => {
        var params = {
          tableName: 'sys_role',
          fieldName: 'name',
          fieldVal: value,
          dataId: this.dataForm.id
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback("角色名已存在!")
          }
        })
      }
      return {
        name: [
          { required: true, message: '请输入角色名称!' },
          { validator: validateName, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        Promise.all([
          this.getMenuList(),
          this.getDeptList()
        ]).then(() => {
          if (this.dataForm.id) {
            this.getById()
          }
        })
      })
    },
    getById() {
      queryRoleById(this.dataForm.id).then((res) => {
        if (res.success) {
          this.dataForm = res.result
        }
      })
    },
    // 获取菜单列表
    getMenuList () {
      return new Promise((resolve, reject) => {
        queryMenuTreeList().then((res) => {
          if (res.success) {
            this.menuList = res.result
          }
          resolve()
        }).catch(() => {
          reject()
        })
      })
    },
    // 获取部门列表
    getDeptList () {
      return new Promise((resolve, reject) => {
        queryDepartTreeList().then((res) => {
          if (res.success) {
            this.deptList = res.result
          }
          resolve()
        }).catch(() => {
          reject()
        })
      })
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          saveAction(this.url.save, that.dataForm).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok');
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.handleCancel();
          });
        }
      })
    },
    handleCancel() {
      this.visible = false
    }
  }
}
</script>

<style scoped>

</style>
